package cn.itlight.org.service.impl;

import cn.itlight.basic.service.impl.BaseServiceImpl;
import cn.itlight.basic.utils.MD5Utils;
import cn.itlight.basic.utils.StrUtils;
import cn.itlight.org.domain.Employee;
import cn.itlight.org.mapper.EmployeeMapper;
import cn.itlight.org.service.IEmployeeService;
import cn.itlight.user.domain.Logininfo;
import cn.itlight.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Description: TODO
* @Author: TomLiu
* @Version: 1.0
* @Date:  2022/6/21 18:41  
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
        initSaveTable(employee);
    }

    /*
    * @Title: initSaveTable
    * @Description: 新增员工同时新增角色
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/13 12:51
    * @Parameters:
    * @Return
    */
    private void initSaveTable(Employee employee) {
        // 判断是否修改角色
        if (employee.getRoles()!= null){
            // 在角色员工中间表新增修改的数据
            employeeMapper.saveRoleEmployee(employee.getId(), employee.getRoles());
        }
    }



    @Override
    public void update(Employee employee) {
        Employee employee1 = employeeMapper.loadById(employee.getId());
        if (!employee1.getPassword().equals(employee.getPassword())){
            String salt = employee1.getSalt();
            String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
            employee.setPassword(saltPassword);
            employee.setSalt(salt);
        }
        employeeMapper.update(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = logininfoMapper.loadById(employee.getLogininfoId());
        Long id = logininfo.getId();
        logininfo = employee2Logininfo(employee);
        logininfo.setId(id);
        logininfoMapper.update(logininfo);
        initRaletionTable(employee);
    }

    /*
    * @Title: initRaletionTable
    * @Description: 修改员工同时修改角色
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/13 12:50
    * @Parameters:
    * @Return
    */
    private void initRaletionTable(Employee employee) {
        // 判断是否修改角色
        if (employee.getRoles() != null){
            // 删除角色员工中间表的数据
            employeeMapper.removeRoleEmployee(employee.getId());
            // 在角色员工中间表新增修改的数据
            employeeMapper.saveRoleEmployee(employee.getId(), employee.getRoles());
        }
    }

    @Override
    public void remove(Long id) {
        initRemoveTable(id);
        Employee employee = employeeMapper.loadById(id);
        employeeMapper.remove(id);
        logininfoMapper.remove(employee.getLogininfoId());
    }

    /*
    * @Title: initRemoveTable
    * @Description: 删除员工同时删除角色
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/13 12:49
    * @Parameters:
    * @Return
    */
    private void initRemoveTable(Long id) {
        employeeMapper.removeRoleEmployee(id);
    }


    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(saltPassword);
        employee.setSalt(salt);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}

